home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zusparam.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  16.2 KB  |  654 lines

  1. /* Copyright (C) 1996, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zusparam.c,v 1.3 2000/09/19 19:00:55 lpd Exp $ */
  20. /* User and system parameter operators */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "oper.h"
  25. #include "gscdefs.h"
  26. #include "gsstruct.h"        /* for gxht.h */
  27. #include "gsfont.h"        /* for user params */
  28. #include "gxht.h"        /* for user params */
  29. #include "gsutil.h"
  30. #include "estack.h"
  31. #include "ialloc.h"        /* for imemory for status */
  32. #include "icontext.h"        /* for set_user_params prototype */
  33. #include "idict.h"
  34. #include "idparam.h"
  35. #include "iparam.h"
  36. #include "dstack.h"
  37. #include "iname.h"
  38. #include "itoken.h"
  39. #include "iutil2.h"
  40. #include "ivmem2.h"
  41. #include "store.h"
  42.  
  43. /* The (global) font directory */
  44. extern gs_font_dir *ifont_dir;    /* in zfont.c */
  45.  
  46. /* Define an individual user or system parameter. */
  47. /* Eventually this will be made public. */
  48. #define param_def_common\
  49.     const char *pname
  50. typedef struct param_def_s {
  51.     param_def_common;
  52. } param_def_t;
  53. typedef struct long_param_def_s {
  54.     param_def_common;
  55.     long min_value, max_value;
  56.     long (*current)(P1(i_ctx_t *));
  57.     int (*set)(P2(i_ctx_t *, long));
  58. } long_param_def_t;
  59.  
  60. #if arch_sizeof_long > arch_sizeof_int
  61. #  define MAX_UINT_PARAM max_uint
  62. #else
  63. #  define MAX_UINT_PARAM max_long
  64. #endif
  65. typedef struct bool_param_def_s {
  66.     param_def_common;
  67.     bool (*current)(P1(i_ctx_t *));
  68.     int (*set)(P2(i_ctx_t *, bool));
  69. } bool_param_def_t;
  70. typedef struct string_param_def_s {
  71.     param_def_common;
  72.     void (*current)(P2(i_ctx_t *, gs_param_string *));
  73.     int (*set)(P2(i_ctx_t *, gs_param_string *));
  74. } string_param_def_t;
  75.  
  76. /* Define a parameter set (user or system). */
  77. typedef struct param_set_s {
  78.     const long_param_def_t *long_defs;
  79.     uint long_count;
  80.     const bool_param_def_t *bool_defs;
  81.     uint bool_count;
  82.     const string_param_def_t *string_defs;
  83.     uint string_count;
  84. } param_set;
  85.  
  86. /* Forward references */
  87. private int setparams(P3(i_ctx_t *, gs_param_list *, const param_set *));
  88. private int currentparams(P2(i_ctx_t *, const param_set *));
  89. private int currentparam1(P2(i_ctx_t *, const param_set *));
  90.  
  91. /* ------ Passwords ------ */
  92.  
  93. /* <string|int> .checkpassword <0|1|2> */
  94. private int
  95. zcheckpassword(i_ctx_t *i_ctx_p)
  96. {
  97.     os_ptr op = osp;
  98.     ref params[2];
  99.     array_param_list list;
  100.     gs_param_list *const plist = (gs_param_list *)&list;
  101.     int result = 0;
  102.     int code = name_ref((const byte *)"Password", 8, ¶ms[0], 0);
  103.     password pass;
  104.  
  105.     if (code < 0)
  106.     return code;
  107.     params[1] = *op;
  108.     array_param_list_read(&list, params, 2, NULL, false, iimemory);
  109.     if (dict_read_password(&pass, systemdict, "StartJobPassword") >= 0 &&
  110.     param_check_password(plist, &pass) == 0
  111.     )
  112.     result = 1;
  113.     if (dict_read_password(&pass, systemdict, "SystemParamsPassword") >= 0 &&
  114.     param_check_password(plist, &pass) == 0
  115.     )
  116.     result = 2;
  117.     iparam_list_release(&list);
  118.     make_int(op, result);
  119.     return 0;
  120. }
  121.  
  122. /* ------ System parameters ------ */
  123.  
  124. /* Integer values */
  125. private long
  126. current_BuildTime(i_ctx_t *i_ctx_p)
  127. {
  128.     return gs_buildtime;
  129. }
  130. private long
  131. current_MaxFontCache(i_ctx_t *i_ctx_p)
  132. {
  133.     return gs_currentcachesize(ifont_dir);
  134. }
  135. private int
  136. set_MaxFontCache(i_ctx_t *i_ctx_p, long val)
  137. {
  138.     return gs_setcachesize(ifont_dir,
  139.                (uint)(val < 0 ? 0 : val > max_uint ? max_uint :
  140.                    val));
  141. }
  142. private long
  143. current_CurFontCache(i_ctx_t *i_ctx_p)
  144. {
  145.     uint cstat[7];
  146.  
  147.     gs_cachestatus(ifont_dir, cstat);
  148.     return cstat[0];
  149. }
  150. private long
  151. current_MaxGlobalVM(i_ctx_t *i_ctx_p)
  152. {
  153.     gs_memory_gc_status_t stat;
  154.  
  155.     gs_memory_gc_status(iimemory_global, &stat);
  156.     return stat.max_vm;
  157. }
  158. private int
  159. set_MaxGlobalVM(i_ctx_t *i_ctx_p, long val)
  160. {
  161.     gs_memory_gc_status_t stat;
  162.  
  163.     gs_memory_gc_status(iimemory_global, &stat);
  164.     stat.max_vm = max(val, 0);
  165.     gs_memory_set_gc_status(iimemory_global, &stat);
  166.     return 0;
  167. }
  168. private long
  169. current_Revision(i_ctx_t *i_ctx_p)
  170. {
  171.     return gs_revision;
  172. }
  173. private const long_param_def_t system_long_params[] =
  174. {
  175.     {"BuildTime", min_long, max_long, current_BuildTime, NULL},
  176. {"MaxFontCache", 0, MAX_UINT_PARAM, current_MaxFontCache, set_MaxFontCache},
  177.     {"CurFontCache", 0, MAX_UINT_PARAM, current_CurFontCache, NULL},
  178.     {"Revision", min_long, max_long, current_Revision, NULL},
  179.     /* Extensions */
  180.     {"MaxGlobalVM", 0, max_long, current_MaxGlobalVM, set_MaxGlobalVM}
  181. };
  182.  
  183. /* Boolean values */
  184. private bool
  185. current_ByteOrder(i_ctx_t *i_ctx_p)
  186. {
  187.     return !arch_is_big_endian;
  188. }
  189. private const bool_param_def_t system_bool_params[] =
  190. {
  191.     {"ByteOrder", current_ByteOrder, NULL}
  192. };
  193.  
  194. /* String values */
  195. private void
  196. current_RealFormat(i_ctx_t *i_ctx_p, gs_param_string * pval)
  197. {
  198. #if arch_floats_are_IEEE
  199.     static const char *const rfs = "IEEE";
  200. #else
  201.     static const char *const rfs = "not IEEE";
  202. #endif
  203.  
  204.     pval->data = (const byte *)rfs;
  205.     pval->size = strlen(rfs);
  206.     pval->persistent = true;
  207. }
  208. private const string_param_def_t system_string_params[] =
  209. {
  210.     {"RealFormat", current_RealFormat, NULL}
  211. };
  212.  
  213. /* The system parameter set */
  214. private const param_set system_param_set =
  215. {
  216.     system_long_params, countof(system_long_params),
  217.     system_bool_params, countof(system_bool_params),
  218.     system_string_params, countof(system_string_params)
  219. };
  220.  
  221. /* <dict> .setsystemparams - */
  222. private int
  223. zsetsystemparams(i_ctx_t *i_ctx_p)
  224. {
  225.     os_ptr op = osp;
  226.     int code;
  227.     dict_param_list list;
  228.     gs_param_list *const plist = (gs_param_list *)&list;
  229.     password pass;
  230.  
  231.     check_type(*op, t_dictionary);
  232.     code = dict_param_list_read(&list, op, NULL, false, iimemory);
  233.     if (code < 0)
  234.     return code;
  235.     code = dict_read_password(&pass, systemdict, "SystemParamsPassword");
  236.     if (code < 0)
  237.     return code;
  238.     code = param_check_password(plist, &pass);
  239.     if (code != 0) {
  240.     if (code > 0)
  241.         code = gs_note_error(e_invalidaccess);
  242.     goto out;
  243.     }
  244.     code = param_read_password(plist, "StartJobPassword", &pass);
  245.     switch (code) {
  246.     default:        /* invalid */
  247.         goto out;
  248.     case 1:        /* missing */
  249.         break;
  250.     case 0:
  251.         code = dict_write_password(&pass, systemdict,
  252.                        "StartJobPassword");
  253.         if (code < 0)
  254.         goto out;
  255.     }
  256.     code = param_read_password(plist, "SystemParamsPassword", &pass);
  257.     switch (code) {
  258.     default:        /* invalid */
  259.         goto out;
  260.     case 1:        /* missing */
  261.         break;
  262.     case 0:
  263.         code = dict_write_password(&pass, systemdict,
  264.                        "SystemParamsPassword");
  265.         if (code < 0)
  266.         goto out;
  267.     }
  268.     code = setparams(i_ctx_p, plist, &system_param_set);
  269.   out:
  270.     iparam_list_release(&list);
  271.     if (code < 0)
  272.     return code;
  273.     pop(1);
  274.     return 0;
  275. }
  276.  
  277. /* - .currentsystemparams <name1> <value1> ... */
  278. private int
  279. zcurrentsystemparams(i_ctx_t *i_ctx_p)
  280. {
  281.     return currentparams(i_ctx_p, &system_param_set);
  282. }
  283.  
  284. /* <name> .getsystemparam <value> */
  285. private int
  286. zgetsystemparam(i_ctx_t *i_ctx_p)
  287. {
  288.     return currentparam1(i_ctx_p, &system_param_set);
  289. }
  290.  
  291. /* ------ User parameters ------ */
  292.  
  293. /* Integer values */
  294. private long
  295. current_JobTimeout(i_ctx_t *i_ctx_p)
  296. {
  297.     return 0;
  298. }
  299. private int
  300. set_JobTimeout(i_ctx_t *i_ctx_p, long val)
  301. {
  302.     return 0;
  303. }
  304. private long
  305. current_MaxFontItem(i_ctx_t *i_ctx_p)
  306. {
  307.     return gs_currentcacheupper(ifont_dir);
  308. }
  309. private int
  310. set_MaxFontItem(i_ctx_t *i_ctx_p, long val)
  311. {
  312.     return gs_setcacheupper(ifont_dir, val);
  313. }
  314. private long
  315. current_MinFontCompress(i_ctx_t *i_ctx_p)
  316. {
  317.     return gs_currentcachelower(ifont_dir);
  318. }
  319. private int
  320. set_MinFontCompress(i_ctx_t *i_ctx_p, long val)
  321. {
  322.     return gs_setcachelower(ifont_dir, val);
  323. }
  324. private long
  325. current_MaxOpStack(i_ctx_t *i_ctx_p)
  326. {
  327.     return ref_stack_max_count(&o_stack);
  328. }
  329. private int
  330. set_MaxOpStack(i_ctx_t *i_ctx_p, long val)
  331. {
  332.     return ref_stack_set_max_count(&o_stack, val);
  333. }
  334. private long
  335. current_MaxDictStack(i_ctx_t *i_ctx_p)
  336. {
  337.     return ref_stack_max_count(&d_stack);
  338. }
  339. private int
  340. set_MaxDictStack(i_ctx_t *i_ctx_p, long val)
  341. {
  342.     return ref_stack_set_max_count(&d_stack, val);
  343. }
  344. private long
  345. current_MaxExecStack(i_ctx_t *i_ctx_p)
  346. {
  347.     return ref_stack_max_count(&e_stack);
  348. }
  349. private int
  350. set_MaxExecStack(i_ctx_t *i_ctx_p, long val)
  351. {
  352.     return ref_stack_set_max_count(&e_stack, val);
  353. }
  354. private long
  355. current_MaxLocalVM(i_ctx_t *i_ctx_p)
  356. {
  357.     gs_memory_gc_status_t stat;
  358.  
  359.     gs_memory_gc_status(iimemory_local, &stat);
  360.     return stat.max_vm;
  361. }
  362. private int
  363. set_MaxLocalVM(i_ctx_t *i_ctx_p, long val)
  364. {
  365.     gs_memory_gc_status_t stat;
  366.  
  367.     gs_memory_gc_status(iimemory_local, &stat);
  368.     stat.max_vm = max(val, 0);
  369.     gs_memory_set_gc_status(iimemory_local, &stat);
  370.     return 0;
  371. }
  372. private long
  373. current_VMReclaim(i_ctx_t *i_ctx_p)
  374. {
  375.     gs_memory_gc_status_t gstat, lstat;
  376.  
  377.     gs_memory_gc_status(iimemory_global, &gstat);
  378.     gs_memory_gc_status(iimemory_local, &lstat);
  379.     return (!gstat.enabled ? -2 : !lstat.enabled ? -1 : 0);
  380. }
  381. private long
  382. current_VMThreshold(i_ctx_t *i_ctx_p)
  383. {
  384.     gs_memory_gc_status_t stat;
  385.  
  386.     gs_memory_gc_status(iimemory_local, &stat);
  387.     return stat.vm_threshold;
  388. }
  389. private long
  390. current_WaitTimeout(i_ctx_t *i_ctx_p)
  391. {
  392.     return 0;
  393. }
  394. private int
  395. set_WaitTimeout(i_ctx_t *i_ctx_p, long val)
  396. {
  397.     return 0;
  398. }
  399. private long
  400. current_MinScreenLevels(i_ctx_t *i_ctx_p)
  401. {
  402.     return gs_currentminscreenlevels();
  403. }
  404. private int
  405. set_MinScreenLevels(i_ctx_t *i_ctx_p, long val)
  406. {
  407.     gs_setminscreenlevels((uint) val);
  408.     return 0;
  409. }
  410. private const long_param_def_t user_long_params[] =
  411. {
  412.     {"JobTimeout", 0, MAX_UINT_PARAM,
  413.      current_JobTimeout, set_JobTimeout},
  414.     {"MaxFontItem", 0, MAX_UINT_PARAM,
  415.      current_MaxFontItem, set_MaxFontItem},
  416.     {"MinFontCompress", 0, MAX_UINT_PARAM,
  417.      current_MinFontCompress, set_MinFontCompress},
  418.     {"MaxOpStack", 0, MAX_UINT_PARAM,
  419.      current_MaxOpStack, set_MaxOpStack},
  420.     {"MaxDictStack", 0, MAX_UINT_PARAM,
  421.      current_MaxDictStack, set_MaxDictStack},
  422.     {"MaxExecStack", 0, MAX_UINT_PARAM,
  423.      current_MaxExecStack, set_MaxExecStack},
  424.     {"MaxLocalVM", 0, max_long,
  425.      current_MaxLocalVM, set_MaxLocalVM},
  426.     {"VMReclaim", -2, 0,
  427.      current_VMReclaim, set_vm_reclaim},
  428.     {"VMThreshold", -1, max_long,
  429.      current_VMThreshold, set_vm_threshold},
  430.     {"WaitTimeout", 0, MAX_UINT_PARAM,
  431.      current_WaitTimeout, set_WaitTimeout},
  432.     /* Extensions */
  433.     {"MinScreenLevels", 0, MAX_UINT_PARAM,
  434.      current_MinScreenLevels, set_MinScreenLevels}
  435. };
  436.  
  437. /* Boolean values */
  438. private bool
  439. current_AccurateScreens(i_ctx_t *i_ctx_p)
  440. {
  441.     return gs_currentaccuratescreens();
  442. }
  443. private int
  444. set_AccurateScreens(i_ctx_t *i_ctx_p, bool val)
  445. {
  446.     gs_setaccuratescreens(val);
  447.     return 0;
  448. }
  449. private const bool_param_def_t user_bool_params[] =
  450. {
  451.     {"AccurateScreens", current_AccurateScreens, set_AccurateScreens}
  452. };
  453.  
  454. /* The user parameter set */
  455. private const param_set user_param_set =
  456. {
  457.     user_long_params, countof(user_long_params),
  458.     user_bool_params, countof(user_bool_params),
  459.     0, 0
  460. };
  461.  
  462. /* <dict> .setuserparams - */
  463. /* We break this out for use when switching contexts. */
  464. int
  465. set_user_params(i_ctx_t *i_ctx_p, const ref *paramdict)
  466. {
  467.     dict_param_list list;
  468.     int code;
  469.  
  470.     check_type(*paramdict, t_dictionary);
  471.     code = dict_param_list_read(&list, paramdict, NULL, false, iimemory);
  472.     if (code < 0)
  473.     return code;
  474.     code = setparams(i_ctx_p, (gs_param_list *)&list, &user_param_set);
  475.     iparam_list_release(&list);
  476.     return code;
  477. }
  478. private int
  479. zsetuserparams(i_ctx_t *i_ctx_p)
  480. {
  481.     os_ptr op = osp;
  482.     int code = set_user_params(i_ctx_p, op);
  483.  
  484.     if (code >= 0) {
  485.     /* Update cached scanner options. */
  486.     i_ctx_p->scanner_options =
  487.         ztoken_scanner_options(op, i_ctx_p->scanner_options);
  488.     pop(1);
  489.     }
  490.     return code;
  491. }
  492.  
  493. /* - .currentuserparams <name1> <value1> ... */
  494. private int
  495. zcurrentuserparams(i_ctx_t *i_ctx_p)
  496. {
  497.     return currentparams(i_ctx_p, &user_param_set);
  498. }
  499.  
  500. /* <name> .getuserparam <value> */
  501. private int
  502. zgetuserparam(i_ctx_t *i_ctx_p)
  503. {
  504.     return currentparam1(i_ctx_p, &user_param_set);
  505. }
  506.  
  507. /* ------ Initialization procedure ------ */
  508.  
  509. const op_def zusparam_op_defs[] =
  510. {
  511.     /* User and system parameters are accessible even in Level 1 */
  512.     /* (if this is a Level 2 system). */
  513.     {"0.currentsystemparams", zcurrentsystemparams},
  514.     {"0.currentuserparams", zcurrentuserparams},
  515.     {"1.getsystemparam", zgetsystemparam},
  516.     {"1.getuserparam", zgetuserparam},
  517.     {"1.setsystemparams", zsetsystemparams},
  518.     {"1.setuserparams", zsetuserparams},
  519.     /* The rest of the operators are defined only in Level 2. */
  520.     op_def_begin_level2(),
  521.     {"1.checkpassword", zcheckpassword},
  522.     op_def_end(0)
  523. };
  524.  
  525. /* ------ Internal procedures ------ */
  526.  
  527. /* Set the values of a parameter set from a parameter list. */
  528. /* We don't attempt to back out if anything fails. */
  529. private int
  530. setparams(i_ctx_t *i_ctx_p, gs_param_list * plist, const param_set * pset)
  531. {
  532.     int i, code;
  533.  
  534.     for (i = 0; i < pset->long_count; i++) {
  535.     const long_param_def_t *pdef = &pset->long_defs[i];
  536.     long val;
  537.  
  538.     if (pdef->set == NULL)
  539.         continue;
  540.     code = param_read_long(plist, pdef->pname, &val);
  541.     switch (code) {
  542.         default:        /* invalid */
  543.         return code;
  544.         case 1:        /* missing */
  545.         break;
  546.         case 0:
  547.         if (val < pdef->min_value || val > pdef->max_value)
  548.             return_error(e_rangecheck);
  549.         code = (*pdef->set)(i_ctx_p, val);
  550.         if (code < 0)
  551.             return code;
  552.     }
  553.     }
  554.     for (i = 0; i < pset->bool_count; i++) {
  555.     const bool_param_def_t *pdef = &pset->bool_defs[i];
  556.     bool val;
  557.  
  558.     if (pdef->set == NULL)
  559.         continue;
  560.     code = param_read_bool(plist, pdef->pname, &val);
  561.     if (code == 0)
  562.         code = (*pdef->set)(i_ctx_p, val);
  563.     if (code < 0)
  564.         return code;
  565.     }
  566. /****** WE SHOULD DO STRINGS AND STRING ARRAYS, BUT WE DON'T YET ******/
  567.     return 0;
  568. }
  569.  
  570. /* Get the current values of a parameter set to the stack. */
  571. private bool
  572. pname_matches(const char *pname, const ref * psref)
  573. {
  574.     return
  575.     (psref == 0 ||
  576.      !bytes_compare((const byte *)pname, strlen(pname),
  577.             psref->value.const_bytes, r_size(psref)));
  578. }
  579. private int
  580. current_param_list(i_ctx_t *i_ctx_p, const param_set * pset,
  581.            const ref * psref /*t_string */ )
  582. {
  583.     stack_param_list list;
  584.     gs_param_list *const plist = (gs_param_list *)&list;
  585.     int i;
  586.  
  587.     stack_param_list_write(&list, &o_stack, NULL, iimemory);
  588.     for (i = 0; i < pset->long_count; i++) {
  589.     const char *pname = pset->long_defs[i].pname;
  590.  
  591.     if (pname_matches(pname, psref)) {
  592.         long val = (*pset->long_defs[i].current)(i_ctx_p);
  593.         int code = param_write_long(plist, pname, &val);
  594.  
  595.         if (code < 0)
  596.         return code;
  597.     }
  598.     }
  599.     for (i = 0; i < pset->bool_count; i++) {
  600.     const char *pname = pset->bool_defs[i].pname;
  601.  
  602.     if (pname_matches(pname, psref)) {
  603.         bool val = (*pset->bool_defs[i].current)(i_ctx_p);
  604.         int code = param_write_bool(plist, pname, &val);
  605.  
  606.         if (code < 0)
  607.         return code;
  608.     }
  609.     }
  610.     for (i = 0; i < pset->string_count; i++) {
  611.     const char *pname = pset->string_defs[i].pname;
  612.  
  613.     if (pname_matches(pname, psref)) {
  614.         gs_param_string val;
  615.         int code;
  616.  
  617.         (*pset->string_defs[i].current)(i_ctx_p, &val);
  618.         code = param_write_string(plist, pname, &val);
  619.         if (code < 0)
  620.         return code;
  621.     }
  622.     }
  623.     return 0;
  624. }
  625.  
  626. /* Get the current values of a parameter set to the stack. */
  627. private int
  628. currentparams(i_ctx_t *i_ctx_p, const param_set * pset)
  629. {
  630.     return current_param_list(i_ctx_p, pset, NULL);
  631. }
  632.  
  633. /* Get the value of a single parameter to the stack, or signal an error. */
  634. private int
  635. currentparam1(i_ctx_t *i_ctx_p, const param_set * pset)
  636. {
  637.     os_ptr op = osp;
  638.     ref sref;
  639.     int code;
  640.  
  641.     check_type(*op, t_name);
  642.     check_ostack(2);
  643.     name_string_ref((const ref *)op, &sref);
  644.     code = current_param_list(i_ctx_p, pset, &sref);
  645.     if (code < 0)
  646.     return code;
  647.     if (osp == op)
  648.     return_error(e_undefined);
  649.     /* We know osp == op + 2. */
  650.     ref_assign(op, op + 2);
  651.     pop(2);
  652.     return code;
  653. }
  654.